package com.ycb.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycb.common.Result;
import com.ycb.dto.DishDto;
import com.ycb.exception.CustomException;
import com.ycb.pojo.Dish;
import com.ycb.service.IDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品管理 前端控制器
 * </p>
 *
 * @author ycb
 * @since 2024-09-30
 */
@RestController
@RequestMapping("/dish")
@Transactional(rollbackFor = RuntimeException.class)
@Slf4j
public class DishController {
    @Autowired
    private IDishService dishService;

    @GetMapping("/page")
    public Result<Page> page(int page,int pageSize,String name) throws Exception {

        return dishService.pageDishDto(page,pageSize,name);
    }

    @GetMapping("/{id}")
    public Result<DishDto> getById(@PathVariable Long id) throws Exception {
        DishDto byId = dishService.getWithFlavorById(id);
        return Result.success(byId);
    }

    @PutMapping
    public Result<String> update(@RequestBody DishDto dishDto) throws Exception {
        boolean b = dishService.updateWithFlavor(dishDto);
        if (b) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    @PostMapping
    public Result<String> save(@RequestBody DishDto dishDto) throws Exception {
        //保存菜品风味
        boolean save = dishService.saveWithFlavor(dishDto);
        if (save) {
            return Result.success("新增菜品成功");
        } else {
            return Result.error("新增菜品失败");
        }
    }

    @PostMapping("/status/{status}")
    public Result<String> status(@PathVariable Integer status, @RequestParam List<Long> ids) {
        log.info("status:{},ids:{}", status, ids);
        LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(ids != null, Dish::getId, ids);
        updateWrapper.set(Dish::getStatus, status);
        dishService.update(updateWrapper);
        return Result.success("批量操作成功");
    }

    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids) {
        log.info("删除的ids：{}", ids);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId, ids);
        queryWrapper.eq(Dish::getStatus, 1);
        long count = dishService.count(queryWrapper);
        if (count > 0) {
            throw new CustomException("删除列表中存在启售状态商品，无法删除");
        }
        dishService.removeWithFlavorByIds(ids);
        return Result.success("删除成功");
    }

    @GetMapping("/list")
    public Result<List<DishDto>> get(@RequestParam String categoryId) {

        return dishService.listByCategoryId(categoryId);
    }
}
